/* JavaLog - package tracker 
 * 
 * Copyright (C) 2009  Allyson Lima, Diego Pedro, Victor Freire
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software 
 * Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this 
 * program.  If not, see <http://www.gnu.org/licenses/>.
 */
package br.edu.ufcg.ccc.javalog.view;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import br.edu.ufcg.ccc.javalog.comments.PossibleImprovement;
import br.edu.ufcg.ccc.javalog.controller.HeadquartersController;
import br.edu.ufcg.ccc.javalog.exceptions.HeadquartersControllerException;
import br.edu.ufcg.ccc.javalog.model.Address;
import br.edu.ufcg.ccc.javalog.model.Headquarters;
import br.edu.ufcg.ccc.javalog.model.Validator;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
/** 
 * Window for adding a new franchise.
 * @author Allyson Lima, Diego Pedro, Victor Freire
 * @version 27/11/09 
 */
@SuppressWarnings("serial") // not serializing this...
public class AddFranchiseView extends AbstractView {
	//private Headquarters headquarters; // model, only used to init the controller
	private HeadquartersController headquartersController; // controller
	
	private JLabel labelCity;
	private AbstractAction actionCancel;
	private JButton buttonCancel;
	private JButton buttonOk;
	private JTextField textNumber;
	private JFormattedTextField textZipcode;
	private JTextField textDistrict;
	private JTextField textStreet;
	private JTextField textCity;
	private JTextField textState;
	private JComboBox comboRegion;
	private JTextField textName;
	private JLabel labelZipcode;
	private JCheckBox checkHasAirport;
	private AbstractAction actionOk;
	private JLabel labelDistrict;
	private JLabel labelNumber;
	private JLabel labelStreet;
	private JLabel labelState;
	private JLabel labelRegion;
	private JLabel labelName;
	
	

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				AddFranchiseView inst = new AddFranchiseView();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	private AddFranchiseView() {
		super();
		initGUI();
	}
	
	/**
	 * Creates this view for the specified headquarters model object.
	 * @param headquarters the model to view
	 */
	public AddFranchiseView(Headquarters headquarters) {
		this();
		Validator.validateObject(headquarters, new IllegalArgumentException("null headquarters received"));
		//this.headquarters = headquarters;
		this.headquartersController = new HeadquartersController(headquarters);
		
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
			getContentPane().setLayout(thisLayout);
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
		.addContainerGap()
		.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
		    .addComponent(getTextName(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
		    .addComponent(getLabelName(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
		.addGap(16)
		.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
		    .addComponent(getComboRegion(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
		    .addComponent(getLabelRegion(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
		.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
		.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
		    .addComponent(getTextState(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
		    .addComponent(getLabelState(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
		.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
		.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
		    .addComponent(getTextCity(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
		    .addComponent(getLabelCity(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
		.addGap(12)
		.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
		    .addComponent(getTextNumber(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
		    .addComponent(getLabelNumber(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
		    .addComponent(getTextStreet(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
		    .addComponent(getLabelStreet(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
		.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
		.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
		    .addComponent(getTextDistrict(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
		    .addComponent(getLabelDistrict(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
		.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
		.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
		    .addComponent(getTextZipcode(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
		    .addComponent(getLabelZipcode(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
		.addGap(13)
		.addComponent(getCheckHasAirport(), GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
		.addGap(0, 30, GroupLayout.PREFERRED_SIZE)
		.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
		    .addComponent(getButtonCancel(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
		    .addComponent(getButtonOk(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
		.addContainerGap());
	thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(getCheckHasAirport(), GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				        .addGap(0, 323, GroupLayout.PREFERRED_SIZE))
				    .addGroup(thisLayout.createSequentialGroup()
				        .addGroup(thisLayout.createParallelGroup()
				            .addComponent(getLabelZipcode(), GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
				            .addComponent(getLabelDistrict(), GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
				            .addComponent(getLabelStreet(), GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
				            .addComponent(getLabelCity(), GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
				            .addComponent(getLabelState(), GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
				            .addComponent(getLabelRegion(), GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
				            .addComponent(getLabelName(), GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE))
				        .addGroup(thisLayout.createParallelGroup()
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addGroup(thisLayout.createParallelGroup()
				                    .addComponent(getTextDistrict(), GroupLayout.Alignment.LEADING, 0, 181, Short.MAX_VALUE)
				                    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                        .addComponent(getTextZipcode(), GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE)
				                        .addGap(74))
				                    .addComponent(getTextCity(), GroupLayout.Alignment.LEADING, 0, 181, Short.MAX_VALUE)
				                    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                        .addComponent(getTextState(), 0, 107, Short.MAX_VALUE)
				                        .addGap(74))
				                    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                        .addComponent(getComboRegion(), GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE)
				                        .addGap(74)))
				                .addGap(0, 118, GroupLayout.PREFERRED_SIZE)
				                .addComponent(getButtonOk(), GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE))
				            .addComponent(getTextStreet(), GroupLayout.Alignment.LEADING, 0, 354, Short.MAX_VALUE)
				            .addComponent(getTextName(), GroupLayout.Alignment.LEADING, 0, 354, Short.MAX_VALUE))))
				.addGap(18)
				.addGroup(thisLayout.createParallelGroup()
				    .addComponent(getButtonCancel(), GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE)
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(getLabelNumber(), GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
				        .addComponent(getTextNumber(), GroupLayout.PREFERRED_SIZE, 57, GroupLayout.PREFERRED_SIZE)))
				.addContainerGap(15, 15));
			this.setTitle("Adicionar Nova Franquia");
			pack();
			this.setSize(554, 362);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	* This method should return an instance of this class which does 
	* NOT initialize it's GUI elements. This method is ONLY required by
	* Jigloo if the superclass of this class is abstract or non-public. It 
	* is not needed in any other situation.
	 */
	public static Object getGUIBuilderInstance() {
		return new AddFranchiseView(Boolean.FALSE);
	}
	
	/**
	 * This constructor is used by the getGUIBuilderInstance method to
	 * provide an instance of this class which has not had it's GUI elements
	 * initialized (ie, initGUI is not called in this constructor).
	 */
	private AddFranchiseView(Boolean initGUI) {
		super();
	}

	private JLabel getLabelName() {
		if(labelName == null) {
			labelName = new JLabel();
			labelName.setText("Nome: ");
		}
		return labelName;
	}
	
	private JLabel getLabelRegion() {
		if(labelRegion == null) {
			labelRegion = new JLabel();
			labelRegion.setText("Região:");
		}
		return labelRegion;
	}
	
	private JLabel getLabelState() {
		if(labelState == null) {
			labelState = new JLabel();
			labelState.setText("Estado:");
		}
		return labelState;
	}
	
	private JLabel getLabelCity() {
		if(labelCity == null) {
			labelCity = new JLabel();
			labelCity.setText("Cidade:");
		}
		return labelCity;
	}
	
	private JLabel getLabelStreet() {
		if(labelStreet == null) {
			labelStreet = new JLabel();
			labelStreet.setText("Endereço:");
		}
		return labelStreet;
	}
	
	private JLabel getLabelNumber() {
		if(labelNumber == null) {
			labelNumber = new JLabel();
			labelNumber.setText("Nº:");
		}
		return labelNumber;
	}
	
	private JLabel getLabelZipcode() {
		if(labelZipcode == null) {
			labelZipcode = new JLabel();
			labelZipcode.setText("CEP:");
		}
		return labelZipcode;
	}
	
	private JLabel getLabelDistrict() {
		if(labelDistrict == null) {
			labelDistrict = new JLabel();
			labelDistrict.setText("Bairro:");
		}
		return labelDistrict;
	}

	private JTextField getTextName() {
		if(textName == null) {
			textName = new JTextField();
		}
		return textName;
	}
	
	private JComboBox getComboRegion() {
		if(comboRegion == null) {
			ComboBoxModel comboRegionModel = 
				new DefaultComboBoxModel(
						Address.getValidRegions().toArray(new String[1]));
			comboRegion = new JComboBox();
			comboRegion.setModel(comboRegionModel);
		}
		return comboRegion;
	}
	
	private JTextField getTextState() {
		if(textState == null) {
			textState = new JTextField();
		}
		return textState;
	}
	
	private JTextField getTextCity() {
		if(textCity == null) {
			textCity = new JTextField();
		}
		return textCity;
	}
	
	private JTextField getTextStreet() {
		if(textStreet == null) {
			textStreet = new JTextField();
		}
		return textStreet;
	}
	
	private JTextField getTextDistrict() {
		if(textDistrict == null) {
			textDistrict = new JTextField();
		}
		return textDistrict;
	}
	
	private JFormattedTextField getTextZipcode() {
		if(textZipcode == null) {
			textZipcode = new JFormattedTextField(ViewUtil.getZipcodeFormatter());
		}
		return textZipcode;
	}
	
	private JTextField getTextNumber() {
		if(textNumber == null) {
			textNumber = new JTextField();
		}
		return textNumber;
	}
	
	private JButton getButtonOk() {
		if(buttonOk == null) {
			buttonOk = new JButton();
			buttonOk.setText("OK");
			buttonOk.setMnemonic(java.awt.event.KeyEvent.VK_O);
			buttonOk.setAction(getActionOk());
		}
		return buttonOk;
	}
	
	private JButton getButtonCancel() {
		if(buttonCancel == null) {
			buttonCancel = new JButton();
			buttonCancel.setText("Cancelar");
			buttonCancel.setMnemonic(java.awt.event.KeyEvent.VK_C);
			buttonCancel.setAction(getActionCancel());
		}
		return buttonCancel;
	}
	
	private AbstractAction getActionCancel() {
		if(actionCancel == null) {
			actionCancel = new AbstractAction("Cancelar", null) {
				public void actionPerformed(ActionEvent evt) {
					dispose();
				}
			};
			actionCancel.putValue(javax.swing.Action.MNEMONIC_KEY, new Integer(java.awt.event.KeyEvent.VK_C));
		}
		return actionCancel;
	}
	
	@PossibleImprovement(
			author = "Victor Freire", 
			date = "01/12/09", 
			description = "Log unexpected errors.")
	private AbstractAction getActionOk() {
		if(actionOk == null) {
			actionOk = new AbstractAction("OK", null) {
				public void actionPerformed(ActionEvent evt) {
					String name = textName.getText(),
						   state = textState.getText(),
						   city = textCity.getText(),
						   street = textStreet.getText(),
						   number = textNumber.getText(),
						   zipcode = textZipcode.getText(),
						   district = textDistrict.getText();
					String region = (String)comboRegion.getSelectedItem();
					boolean hasAirport = checkHasAirport.isSelected();
					
					if(headquartersController == null) {
						JOptionPane.showMessageDialog(AddFranchiseView.this, "Falha ao tentar acessar dados.", "Erro", JOptionPane.ERROR_MESSAGE);
						return;
					}
					
					try {
						headquartersController.addFranchise(name, region, state, city, street, number, zipcode, district, hasAirport);
						JOptionPane.showMessageDialog(AddFranchiseView.this, "Nova franquia adicionada com sucesso!");
						dispose();
					} 
					catch(HeadquartersControllerException e) {
						JOptionPane.showMessageDialog(AddFranchiseView.this, e.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
					}
				}
			};
			actionOk.putValue(javax.swing.Action.MNEMONIC_KEY, new Integer(java.awt.event.KeyEvent.VK_O));
		}
		return actionOk;
	}
	
	private JCheckBox getCheckHasAirport() {
		if(checkHasAirport == null) {
			checkHasAirport = new JCheckBox();
			checkHasAirport.setText("Tem aeroporto:");
			checkHasAirport.setHorizontalTextPosition(SwingConstants.LEFT);
			checkHasAirport.setHideActionText(true);
			checkHasAirport.setHorizontalAlignment(SwingConstants.LEFT);
		}
		return checkHasAirport;
	}

}
